package com.fanbai.score.user.fragemnt;

/**
 * Created by acer on 2017/5/6.
 */

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.NestedScrollView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.fanbai.score.R;
import com.fanbai.score.common.Utils.AppUtils;
import com.fanbai.score.common.Utils.ImageUtils;
import com.fanbai.score.common.Utils.ToastUtils;
import com.fanbai.score.common.activity.LoginActivity;
import com.fanbai.score.common.activity.WebView_Activity;
import com.fanbai.score.common.base.BaseFragment;
import com.fanbai.score.common.http.CallBack;
import com.fanbai.score.common.view.AutoScrollTextView;
import com.fanbai.score.common.zxing.activity.CaptureActivity;
import com.fanbai.score.user.bean.HeadImage_Bean;
import com.fanbai.score.user.bean.NoticeInfo_Bean;
import com.fanbai.score.user.http.UserRequestNetwork_Utils;
import com.jude.rollviewpager.OnItemClickListener;
import com.jude.rollviewpager.RollPagerView;
import com.jude.rollviewpager.adapter.StaticPagerAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * A placeholder fragment containing a simple view.
 */
public class Home_Fragment extends BaseFragment {

    @BindView(R.id.nestedScrollView)
    NestedScrollView nestedScrollView;
    @BindView(R.id.userFragmentMain_baseValue)
    TextView baseValue;
    @BindView(R.id.userFragmentMain_tabs)
    public TabLayout tabLayout;
    @BindView(R.id.userFragmentMain_viewPage)
    public ViewPager mViewPager;
    @BindView(R.id.Tenant_rollPagerView)
    RollPagerView mRollViewPager;
    private SectionsPagerAdapter mSectionsPagerAdapter;
    /*@BindView(R.id.userFragmentMain_listView)
    NoScrollListView listView;*/
    @BindView(R.id.autoScrollTextView)
    AutoScrollTextView autoScrollTextView;

    List<HeadImage_Bean> headImageBeanList;//轮播图数据

    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    private static final String ARG_SECTION_NUMBER = "section_number";

    /**
     * Returns a new instance of this fragment for the given section
     * number.
     */
    public static Home_Fragment newInstance(int sectionNumber) {
        Home_Fragment fragment = new Home_Fragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    public Home_Fragment() {
    }


    @Override
    protected int getLayoutID() {
        return R.layout.user_fragment_home;
    }

    @Override
    public String getTitleName() {
        return "首页";
    }

    @Override
    public void iniTitleLeft(ImageView imageView) {
        imageView.setImageResource(R.mipmap.icon_saosao);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().startActivityForResult(new Intent(ctx, CaptureActivity.class),0);
            }
        });
    }

    @Override
    public void iniTitleRight(TextView tv) {
        tv.setText("退出");
        tv.setTextColor(AppUtils.getResource().getColor(R.color.color_font_black_white));
        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ctx.jumpToActivity(LoginActivity.class);
            }
        });
    }

    @Override
    protected void initView(View view) {
        titleBar.setBackgroundResource(R.color.translucent);
        mSectionsPagerAdapter = new SectionsPagerAdapter(getChildFragmentManager());
        mViewPager.setAdapter(mSectionsPagerAdapter);
        tabLayout.setupWithViewPager(mViewPager);
        nestedScrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
            @Override
            public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                    if(!tabLayout.isShown()){

                    }else{

                    }
            }
        });
    }

    @Override
    protected void initData() {
        getHeadImages();
        getNoticeInfos();
        getBaseValue();
        /*List<String> dataList = new ArrayList<>();
        dataList.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1494156320411&di=ee09c92ad8fa24ca4fe0efb54d902133&imgtype=0&src=http%3A%2F%2Fimg.newmotor.com.cn%2FUploadFiles%2Fimage%2F20160506%2F20160506114832563256.png");
        dataList.add("https://timgsa.baidu.com/timg?image&quality=80&size=b10000_10000&sec=1494146013&di=8cdae46fd79675fb1e22e24572d7f578&src=http://www.moto2s.com/ziliao/59/2012102014411842848.jpg");
        dataList.add("https://timgsa.baidu.com/timg?image&quality=80&size=b10000_10000&sec=1494146013&di=8cdae46fd79675fb1e22e24572d7f578&src=http://www.moto2s.com/ziliao/59/2012102014411842848.jpg");
        listView.setAdapter(new CommonAdapter<String>(ctx, dataList, R.layout.user_item_fragment_main) {
            @Override
            public void convert(ViewHolder helper, String item) {
                helper.setImageByUrl(R.id.userItemFragmentMain_img, ""+item);
            }
        });*/
        /*List<String> dataList2 = new ArrayList<>();
        dataList2.add("11111111111111111111111111");
        dataList2.add("222222222222222222222222");
        dataList2.add("33333333333333333333");
        setAutoScrollTextView(dataList2);*/
    }

    @Override
    protected void initListener() {
        mRollViewPager.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                if (headImageBeanList.size() > position) {
                    HeadImage_Bean bean = headImageBeanList.get(position);
                    if (bean.getUrl() != null && !bean.getUrl().equals("")) {
                        Bundle bundle = new Bundle();
                        bundle.putString("url", "" + bean.getUrl());
                        ctx.jumpToActivity(WebView_Activity.class, bundle);
                    }
                }
            }
        });
    }

    private void setAutoScrollTextView(final List<NoticeInfo_Bean> notice_array) {
        List<View> views = initAutoScroll_Views(notice_array);
        autoScrollTextView.setViews(views);
        /**
         * 设置item_view的监听
         */
        autoScrollTextView
                .setOnItemClickListener(new AutoScrollTextView.OnItemClickListener() {
                    @Override
                    public void onItemClick(int position, View view) {
                        Bundle bundle = new Bundle();
                        bundle.putString("url", notice_array.get(position).getUrl());
                        ctx.jumpToActivity(WebView_Activity.class, bundle);
                    }
                });
    }
    public List<View> initAutoScroll_Views(List<NoticeInfo_Bean> notice_array) {
        List<View> autoScrollViewList = new ArrayList<View>();
        for (int i = 0; i < notice_array.size(); i++) {
            View view = LayoutInflater.from(ctx).inflate(R.layout.include_auto_scroll_textview, null);
            TextView textView = (TextView) view.findViewById(R.id.texts);
            textView.setText(notice_array.get(i).getTitle());
            //添加到循环滚动数组里面去
            autoScrollViewList.add(view);
        }
        return autoScrollViewList;
    }

    private void getHeadImages(){
        UserRequestNetwork_Utils.getHeadImages(ctx, new CallBack<List<HeadImage_Bean>>() {
            @Override
            public void onSuccess(String code, String msg, List<HeadImage_Bean> beans) {
                headImageBeanList = beans;
                //2个Adapter用法一样;
                TestNomalAdapter mAdapter = new TestNomalAdapter(headImageBeanList);
                mRollViewPager.setAdapter(mAdapter);
            }

            @Override
            public void onFailure(String code, String msg, String body) {
                ToastUtils.WarnImageToast(ctx, msg);
            }

            @Override
            public void onError(String code, String msg) {
                ToastUtils.ErrorImageToast(ctx, msg);
            }
        });
    }
    private void getNoticeInfos(){
        UserRequestNetwork_Utils.getNoticeInfos(ctx, new CallBack<List<NoticeInfo_Bean>>() {
            @Override
            public void onSuccess(String code, String msg, List<NoticeInfo_Bean> beans) {
                setAutoScrollTextView(beans);
            }

            @Override
            public void onFailure(String code, String msg, String body) {
                ToastUtils.WarnImageToast(ctx, msg);
            }

            @Override
            public void onError(String code, String msg) {
                ToastUtils.ErrorImageToast(ctx, msg);
            }
        });
    }

    private void getBaseValue(){
        UserRequestNetwork_Utils.getBaseValue(ctx, new CallBack<String>() {
            @Override
            public void onSuccess(String code, String msg, String s) {
                baseValue.setText("今日积分兑换值："+s);
            }

            @Override
            public void onFailure(String code, String msg, String body) {
                ToastUtils.WarnImageToast(ctx, msg);
            }

            @Override
            public void onError(String code, String msg) {
                ToastUtils.ErrorImageToast(ctx, msg);
            }

        });
    }

    private class TestNomalAdapter extends StaticPagerAdapter {
        List<HeadImage_Bean> headImage_been;

        public TestNomalAdapter(List<HeadImage_Bean> headImage_been) {
            this.headImage_been = headImage_been;
        }

        @Override
        public View getView(ViewGroup container, int position) {
            ImageView view = new ImageView(container.getContext());
            ImageUtils.show(ctx, headImage_been.get(position).getImage(),
                    view, R.mipmap.icon_img_defeated);
            view.setScaleType(ImageView.ScaleType.CENTER_CROP);
            view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            return view;
        }

        @Override
        public int getCount() {
            return headImage_been.size();
        }
    }

    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            // getItem is called to instantiate the fragment for the given page.
            // Return a Home_Fragment (defined as a static inner class below).
            switch (position) {
                case 0:
                    return Home_ElegantScore_Fragment.newInstance(position + 1);
                case 1:
                    return Home_Exchange_Fragment.newInstance(position + 1);
            }
            return Home_ElegantScore_Fragment.newInstance(position + 1);
        }

        @Override
        public int getCount() {
            // Show 3 total pages.
            return 2;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            String title = "";
            switch (position){
                case 0:
                    title = "贵人圈";
                    break;
                case 1:
                    title = "贵人兑换";
                    break;
                default:
                    title = "贵人圈";
                    break;
            }
            return title;
        }
    }
}
